package com.jk.kdd.system.controller;

import com.jk.kdd.system.model.SystemUser;
import com.jk.kdd.system.service.SystemRoleService;
import com.jk.kdd.system.service.SystemUserService;
import com.jk.kdd.system.vo.SystemUserVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smart4j.framework.util.CastUtil;
import org.smart4j.framework.util.CodecUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by DELL on 2016/10/9.
 */
@Controller
@RequestMapping("/user")
public class UserManageController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserManageController.class);

    @Resource(name = "systemUserService")
    private SystemUserService systemUserService;
    @Resource(name = "systemRoleService")
    private SystemRoleService systemRoleService;


    /**
     * 修改用户信息画面
     */
    @RequestMapping(value="/personalInfo",method= RequestMethod.GET)
    public String editInfo(HttpServletRequest request, Model model) {

        HttpSession session = request.getSession();

        String operateId = (String )session.getAttribute("operateId");
        String companyUserId = (String)session.getAttribute("companyUserId");

        // 查询用户
        SystemUser user = new SystemUser();
        user.setId(CastUtil.castLong(operateId));
        SystemUserVO userVO = systemUserService.getSystemUserByPK(user);
        model.addAttribute("user",userVO);

        // 当前广告主信息
        SystemUser systemUser = new SystemUser();
        systemUser.setId(CastUtil.castLong(companyUserId));
        SystemUserVO systemUserVO = systemUserService.getSystemUserByPK(systemUser);
        model.addAttribute("currentAdUser",systemUserVO);
        // 管理的广告主用户
        List<SystemUserVO> manageAdUserList = systemUserService.getManageAdSystemUser(operateId);
        model.addAttribute("manageAdUserList",manageAdUserList);

        return "user/user_persional_info";
    }

    /**
     * 修改密码
     */
    @RequestMapping(value="/updatePassword",method= RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updatePassword(HttpServletRequest request, Model model) {

        HttpSession session = request.getSession();

        String operateId = (String )session.getAttribute("operateId");
        String ex_password = (String )request.getParameter("ex_password");
        String password1 = (String )request.getParameter("password1");
        String password2 = (String )request.getParameter("password2");
        // 查询用户
        SystemUser user = new SystemUser();
        user.setId(CastUtil.castLong(operateId));
        user = systemUserService.getSystemUserModelByPK(user);

        Map<String,Object> resultMap = new HashMap<String,Object>();
        // 判断前密码是否正确
        if (!CodecUtil.md5(ex_password).equals(user.getPassword())) {
            resultMap.put("code","2");
            resultMap.put("msg","原始密码输出错误");
        }
        // 判断两次密码是否一致
        else if (!password1.equals(password2)) {
            resultMap.put("code","2");
            resultMap.put("msg","两次密码不一致");
        }
        else {
            user = new SystemUser();
            user.setId(CastUtil.castLong(operateId));
            user.setPassword(CodecUtil.md5(password1));

            resultMap.put("code","1");
            resultMap.put("msg","修改成功");
        }

        return resultMap;
    }



    /**
     * 修改邮箱
     */
    @RequestMapping(value="/updateEmail",method= RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateEmail(HttpServletRequest request, Model model) {

        HttpSession session = request.getSession();

        String operateId = (String )session.getAttribute("operateId");
        String userId = request.getParameter("userId");
        String email = request.getParameter("email");

        SystemUser user = new SystemUser();
        user.setId(CastUtil.castLong(userId));
        user.setEmail(email);

        Map<String,Object> resultMap = new HashMap<String,Object>();
        resultMap.put("code","1");
        resultMap.put("msg","修改成功");

        return resultMap;
    }
}
